package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.zhongqing.ZhongQingData;
import com.ucredit.babel.common.model.third.zhongqing.ZhongQingRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 中青重点人群
 *
 * @author renzhanzheng
 */

public class ZhongQingParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 中青重点人群
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        ZhongQingRecord obj = (ZhongQingRecord)model;
        ZhongQingData ZhongQingdata = obj.getData();
        JSONObject info =ZhongQingdata.getData() == null ? new JSONObject() : JSON.parseObject(ZhongQingdata.getData());
        JSONObject data = info.getJSONObject("data") == null ? new JSONObject() : info.getJSONObject("data");
        JSONObject crimeVerifyScore = data.getJSONObject("crime_verify_score") == null ? new JSONObject() : data.getJSONObject("crime_verify_score");
        JSONObject scoreData = crimeVerifyScore.getJSONObject("data") == null ? new JSONObject() : crimeVerifyScore.getJSONObject("data");

        StringBuilder sb = getBaseInfo(obj, successful);
        //错误码
        sb.append(info.getString("err_code") == null ? "" : info.getString("err_code")).append(CRT001);
        //错误信息
        sb.append(info.getString("err_msg") == null ? "" : info.getString("err_msg")).append(CRT001);
        //订单号
        sb.append(data.getString("query_id") == null ? "" : data.getString("query_id")).append(CRT001);
        //返回状态详情
        sb.append(crimeVerifyScore.getString("msg") == null ? "" : crimeVerifyScore.getString("msg")).append(CRT001);
        //返回状态码
        sb.append(crimeVerifyScore.getString("code") == null ? "" : crimeVerifyScore.getString("code")).append(CRT001);
        //综合风险等级
        sb.append(scoreData.getString("score") == null ? "" : scoreData.getString("score")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_zhongqing_risklevel_inc";
    }

    @Override
    public String getType() {
        return RecordType.ZHONGQING_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "中青重点人群";
    }
}
